﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.Security;
public partial class Shared_login : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {

    }
    protected void Button1_Click(object sender, EventArgs e)
    {
        //Response.Clear();
        //Response.Write("<script language=javascript>window.alert('您没有权限进入本页！\\n请登录或与管理员联系！');history.back();</script>");
        //Response.End();
        string strEmail = email.Value.Trim();
        string strPass = pass.Value;
        Tuan.BLL.UserInfo userobj = new Tuan.BLL.UserInfo();
        if (Membership.ValidateUser(strEmail, strPass))
        {
            userobj.AddUser(strEmail);
            MembershipUser userObj = Membership.GetUser(Context.User.Identity.Name);
            string[] roles = Roles.GetRolesForUser(strEmail);
            string userRoles = "";
            foreach (string item in roles)
            {
                userRoles += item + ",";
            }
            DateTime loginTime = DateTime.Now.AddHours(2);
            if(auto_login.Checked)
                    loginTime = DateTime.Now.AddYears(2);
            FormsAuthenticationTicket Ticket = new FormsAuthenticationTicket(1, strEmail, DateTime.Now,loginTime , false, userRoles.TrimEnd(new char[] { ',' }),"/");
            string HashTicket = FormsAuthentication.Encrypt(Ticket);
            HttpCookie UserCookie = new HttpCookie(FormsAuthentication.FormsCookieName, HashTicket);
            //UserCookie.Domain = "5151tuan.com";
            //UserCookie.Path = "/";
            Response.Cookies.Add(UserCookie);

            if (Context.Request["ReturnUrl"] != null)
            {
                //Response.Redirect();
                Response.Write("<script language = javascript>window.parent.location.href='" + Context.Request["ReturnUrl"] + "'</script>");
            }
            else
            {
                string url = "/Pages/Usercenter/MyOrderList.aspx";
                Response.Write("<script language = javascript>window.parent.location.href='" + url + "'</script>");
                //Response.Write("<script>window.open('" + url + "');</script>");
            }
          
        }
        else
        {
            Pan.Common.MessageBox.Show(this,"输入的帐号或密码有误，请重试!");
        }
    }
}
